package com.wondream.myframework.app.request.gateway.encrypt;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class HyTreeDesECBUtils {
    public static final String Algorithm3DES = "DESede";
    public static final String AlgorithmDES = "DES";
    public static final String CIPHER_ALGORITHM = "desede/ECB/NoPadding";
    private static final byte[] FIX_KEY = hexStringToBytes("00FB00100066609B0FF5E8A6AE75682C");

    public HyTreeDesECBUtils() {
    }

    public static byte[] encryptMode(byte[] src, String Algorithm) {
        try {
            SecretKey deskey = new SecretKeySpec(Algorithm.equals("DESede") ? build3DesKey(FIX_KEY) : build3DesKey(FIX_KEY), Algorithm);
            Cipher c1 = Cipher.getInstance("desede/ECB/NoPadding");
            c1.init(1, deskey);
            return c1.doFinal(src);
        } catch (NoSuchAlgorithmException var4) {
            var4.printStackTrace();
        } catch (NoSuchPaddingException var5) {
            var5.printStackTrace();
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return null;
    }

    public static byte[] encryptMode(byte[] src, byte[] key, String Algorithm) {
        try {
            SecretKey deskey = new SecretKeySpec(Algorithm.equals("DESede") ? build3DesKey(key) : build3DesKey(key), Algorithm);
            Cipher c1 = Cipher.getInstance("desede/ECB/NoPadding");
            c1.init(1, deskey);
            return c1.doFinal(src);
        } catch (NoSuchAlgorithmException var5) {
            var5.printStackTrace();
        } catch (NoSuchPaddingException var6) {
            var6.printStackTrace();
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        return null;
    }

    public static byte[] decryptMode(byte[] src, String Algorithm) {
        try {
            SecretKey deskey = new SecretKeySpec(build3DesKey(FIX_KEY), Algorithm);
            Cipher c1 = Cipher.getInstance("desede/ECB/NoPadding");
            c1.init(2, deskey);
            return c1.doFinal(src);
        } catch (NoSuchAlgorithmException var4) {
            var4.printStackTrace();
        } catch (NoSuchPaddingException var5) {
            var5.printStackTrace();
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return null;
    }

    public static byte[] withoutAutofill(byte[] src) {
        byte[] newbyte = new byte[src.length - 8];

        for(int i = 0; i < newbyte.length; ++i) {
            newbyte[i] = src[i];
        }

        return newbyte;
    }

    public static byte[] build3DesKey(byte[] temp) throws UnsupportedEncodingException {
        byte[] key = new byte[24];
        System.arraycopy(temp, 0, key, 0, temp.length);

        for(int i = 0; i < 8; ++i) {
            key[16 + i] = temp[i];
        }

        return key;
    }

    public static String byte2HexStr(byte[] b) {
        String hs = "";
        String stmp = "";

        for(int n = 0; n < b.length; ++n) {
            stmp = Integer.toHexString(b[n] & 255);
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp + "";
            } else {
                hs = hs + stmp + "";
            }
        }

        return hs.toUpperCase();
    }

    public static byte[] hexStringToBytes(String hexString) {
        if (hexString != null && !hexString.equals("")) {
            hexString = hexString.toUpperCase();
            int length = hexString.length() / 2;
            char[] hexChars = hexString.toCharArray();
            byte[] d = new byte[length];

            for(int i = 0; i < length; ++i) {
                int pos = i * 2;
                d[i] = (byte)(charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
            }

            return d;
        } else {
            return null;
        }
    }

    private static byte charToByte(char c) {
        return (byte)"0123456789ABCDEF".indexOf(c);
    }

    public static void main(String[] args) {
        String msg = "33e006beda22fa9e5ad722bd108afe99d2dd3ca11daedfd2";
        System.out.println("【解密前】：" + msg);
        System.out.println(" keystr " + FIX_KEY.length);
        System.out.println(hexStringToBytes(msg).length);
        byte[] myMsgArr = decryptMode(hexStringToBytes(msg), "DESede");
        System.out.println("【解密后】：" + byte2HexStr(myMsgArr));
        String data = byte2HexStr(myMsgArr);
        byte[] secretArr = encryptMode(hexStringToBytes(data), "DESede");
        System.out.println("【加密后】：" + byte2HexStr(secretArr));
    }
}
